<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Cinema extends CI_Controller {

    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
        $this->load->model('cinema_model');
        $this->load->model('branch_model');
        $template['current_class'] = "cinema";
    }

	public function index()
	{
	    $data['cinemas'] = $this->cinema_model->get_all(20, $this->uri->segment(4));
	    $template['content'] = $this->load->view('admin/cinema', $data, TRUE);
		$this->load->view('admin/template', $template);
	}
	
	public function create()
	{
	    $this->form_validation->set_rules('name','capacity','branch_id','required');
        $this->form_validation->set_error_delimiters('<span class="form-msg-error">', '</span>');
	    $cinema_editor['title'] = "Create Cinema";
	    $cinema_editor['form_url'] = site_url('admin/cinema/create');
	    $cinema_editor['branchs'] = $this->branch_model->get_all();
	    if($this->form_validation->run())
    	{
    	    $name = $this->input->post('name', TRUE);
            $capacity = $this->input->post('capacity', TRUE);
            $branch_id = $this->input->post('branch_id', TRUE);
            $result = $this->cinema_model->create($name, $capacity, $branch_id);
            if ($result) 
            {
                redirect('admin/cinema');
            }else{
                $cinema_editor['errors'] = 'Database error please contact your administrator.';
            }
            
    	}else{
    	    
    	}
    	$template['content'] = $this->load->view('admin/cinema_editor', $cinema_editor, TRUE);
		$this->load->view('admin/template', $template);
    	
	}
	function edit($cinema_id)
    {
        $this->form_validation->set_rules('name','description','branch_id','required');
        $this->form_validation->set_error_delimiters('<span class="form-msg-error">', '</span>');
	    $cinema_editor['title'] = "Edit Cinema";
	    $cinema_editor['form_url'] = site_url('admin/cinema/edit/'.$cinema_id);
	    $cinema_editor['branchs'] = $this->branch_model->get_all();
	    $cinema_editor['cinema'] = $this->cinema_model->get_by_id($cinema_id);
	    if($this->form_validation->run())
    	{
    	    $name = $this->input->post('name', TRUE);
            $capacity = $this->input->post('capacity', TRUE);
            $branch_id = $this->input->post('branch_id', TRUE);
            $result = $this->branch_model->update($cinema_id,$name, $capacity, $branch_id);
            if ($result) 
            {
                redirect('admin/cinema');
            }else{
                $branch_editor['errors'] = 'Database error please contact your administrator.';
            }
            
    	}else{
    	    
    	}
    	$template['content'] = $this->load->view('admin/cinema_editor', $cinema_editor, TRUE);
		$this->load->view('admin/template', $template);
    }
    
    function destroy($cinema_id)
    {
        $this->cinema_model->delete($cinema_id);
        redirect('admin/cinema');
    }
}